<?php 
class authenticat{
	private $_cookie;
	private $_arr;
	private $_flag;

	// Lay cookie
	public function getCookie(){
		$this->_cookie = $_COOKIE['login'];
		return $this->_cookie;
	}


	// Tach cookie thanh cac thanh pha
	public function extractCookie($key){
		$cookie = $this->getCookie();
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$decry = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $cookie, MCRYPT_MODE_ECB,$iv);
			
		$text = explode('|',$decry);
		$data = explode('/', $text[0]);
		array_push($data, $text[1]);
		$this->_arr = $data;
		return $this->_arr;
	}

	public function checkLogin($key){
		$arr = $this->extractCookie($key);
		$hash = $arr[0] . $arr[1];
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		if(strcmp($hash, $arr[3])){
			$this->_flag = true;
		}else{
			$this->_flag = false;
		}
		return $this->_flag;
	}
}
